package com.flong.juc.charter1;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author:liangjl
 * @Date: 2020/3/29-21:16
 * @Eamil:jilongliang@sina.com
 * @Description: CopyOnWriteArrayList与Vector解决java.util.ConcurrentModificationException的异常
 * Vector相对比较重
 */
public class NoSafeTest {

    public static void main(String[] args) {
        //第一种不可取
        //List<String> list = new ArrayList<>();

        // Vector相对比较重,add方法是使用了synchronized进行实现
        //List<String> list = new Vector<>();

        // Collections使用了synchronized (mutex) 实现
        //List<String> list = Collections.synchronizedList(new ArrayList<>());

        //建议使用
        List<String> list = new CopyOnWriteArrayList<>();

        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(list);
            }, String.valueOf(i)).start();
        }

    }
}
